var TableEditable = function () {

    return {

        //主要功能启动模块
        init: function () {
            function restoreRow(oTable, nRow) {
                var aData = oTable.fnGetData(nRow);
                var jqTds = $('>td', nRow);

                for (var i = 0, iLen = jqTds.length; i < iLen; i++) {
                    oTable.fnUpdate(aData[i], nRow, i, false);
                }

                oTable.fnDraw();
            }

            function editRow(oTable, nRow) {
                var aData = oTable.fnGetData(nRow);
                var jqTds = $('>td', nRow);
                
                jqTds[0].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[0] + '">';
                jqTds[1].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[1] + '">';
                jqTds[2].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[2] + '">';
                jqTds[3].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[3] + '">';
                jqTds[4].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[4] + '">';
                jqTds[5].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[5] + '">';
                jqTds[6].innerHTML = '<a class="edit" href="">Save</a>';
                jqTds[7].innerHTML = '<a class="cancel" href="">Cancel</a>';
            }

            function saveRow(oTable, nRow) {
            	
                var jqInputs = $('input', nRow);
                oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
                oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
                oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
                oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
                oTable.fnUpdate(jqInputs[4].value, nRow, 4, false);
                oTable.fnUpdate(jqInputs[5].value, nRow, 5, false);
                oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 6, false);
                oTable.fnUpdate('<a class="delete" href="">Delete</a>', nRow, 7, false);
                oTable.fnDraw();
            }

            function cancelEditRow(oTable, nRow) {
                var jqInputs = $('input', nRow);
                oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
                oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
                oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
                oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
                oTable.fnUpdate(jqInputs[4].value, nRow, 4, false);
                oTable.fnUpdate(jqInputs[5].value, nRow, 5, false);
                oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 6, false);
                oTable.fnDraw();
            }

            var oTable = $('#sample_editable_1').dataTable({
                "aLengthMenu": [
                    [5, 15, 20, -1],
                    [5, 15, 20, "All"] // 改变每页值的变化
                ],
                // 设定的初始值
                "iDisplayLength": 5,
                "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page",
                    "oPaginate": {
                        "sPrevious": "Prev",
                        "sNext": "Next"
                    }
                },
                "aoColumnDefs": [{
                        'bSortable': false,
                        'aTargets': [0]
                    }
                ]
            });

            jQuery('#sample_editable_1_wrapper .dataTables_filter input').addClass("m-wrap medium"); // 修改表搜索输入
            jQuery('#sample_editable_1_wrapper .dataTables_length select').addClass("m-wrap small"); // 修改表每页下拉
            jQuery('#sample_editable_1_wrapper .dataTables_length select').select2({
                showSearchInput : false //隐藏搜索框有特殊的CSS类
            }); // 初始化SELECT2下拉

            var nEditing = null; newBtnClickNum = 0;
            $('#sample_editable_1_new').click(function (e) {
                e.preventDefault();
                /* 限制用户，只能新增一条正在编辑的行 */
                if(newBtnClickNum > 0)
                	return;
                var aiNew = oTable.fnAddData(['', '', '', '', '', '',
                        '<a class="edit" href="">Edit</a>', '<a class="cancel" data-mode="new" href="">Cancel</a>'
                ]);
                var nRow = oTable.fnGetNodes(aiNew[0]);
                editRow(oTable, nRow);
                nEditing = nRow;
                newBtnClickNum = newBtnClickNum + 1;
            });

            $('#sample_editable_1 a.delete').live('click', function (e) {
                e.preventDefault();
                
                if (confirm("确定要删除此行数据？") == false) {
                    return;
                }

                var nRow = $(this).parents('tr')[0];
                
                /* 删除行 */
                var platesId = nRow.cells[0].innerHTML;
                $.post(
                		"wx!deletePlatesById.action",
                    	{platesId:platesId},
	                    function(re){
                    		if(re == "Y")
                    			oTable.fnDeleteRow(nRow);
                    		else if(re == "N")
                    			alert("删除失败");
	                    }
                );
            });

            $('#sample_editable_1 a.cancel').live('click', function (e) {
                e.preventDefault();
                if ($(this).attr("data-mode") == "new") {
                    var nRow = $(this).parents('tr')[0];
                    oTable.fnDeleteRow(nRow);
                    /* 取消新增行时，允许再次增加行 */
                    newBtnClickNum = 0;
                } else {
                    restoreRow(oTable, nEditing);
                    nEditing = null;
                }
            });

            $('#sample_editable_1 a.edit').live('click', function (e) {
                e.preventDefault();

                /* 获取点击的行 */
                var nRow = $(this).parents('tr')[0];

                if (nEditing !== null && nEditing != nRow) {
                    /* 当前正在编辑 - 但不是这行 - 继续编辑模式之前恢复旧模式 */
                    restoreRow(oTable, nEditing);
                    editRow(oTable, nRow);
                    nEditing = nRow;
                } else if (nEditing == nRow && this.innerHTML == "Save") {
                	/* 编辑此行，并将其保存 */
                    saveRow(oTable, nEditing);
                    nEditing = null;
                    
                    /* 添加、修改行 */
                    var platesId = nRow.cells[0].innerHTML;
                    var platesOrderNo = nRow.cells[1].innerHTML;
                    var platesName = nRow.cells[2].innerHTML;
                    var platesHrefClass = nRow.cells[3].innerHTML;
                    var platesFontClass = nRow.cells[4].innerHTML;
                    var platesUrl = nRow.cells[5].innerHTML;
                    if(platesOrderNo != "" && platesName != ""){
	                    $.post(
	                    	"wx!savePlates.action",
	                    	{platesOrderNo:platesOrderNo,platesId:platesId,platesName:platesName,
	                    		platesHrefClass:platesHrefClass,platesFontClass:platesFontClass,platesUrl:platesUrl},
		                    function(re){
	                    		var json = eval("(" + re + ")"); 
	                    		nRow.cells[0].innerHTML = json.platesId;
	                            nRow.cells[1].innerHTML = json.platesOrderNo;
	                            nRow.cells[2].innerHTML = json.platesName;
	                            nRow.cells[3].innerHTML = json.platesHrefClass;
	                            nRow.cells[4].innerHTML = json.platesFontClass;
	                            nRow.cells[5].innerHTML = json.platesUrl;
	                            
	                            /* 保存完新增行时，允许再次增加行 */
	                            newBtnClickNum = 0;
		                    }
	                    );
                    }else if(platesOrderNo == "" || platesName == ""){
                    	alert("请填写必填项");
                    	editRow(oTable, nRow);
                    	nEditing = nRow;
                    }
                    
                } else {
                    /* 没有进行中的编辑 */
                    editRow(oTable, nRow);
                    nEditing = nRow;
                }
            });
        }

    };

}();